package com.kuizii.server.config;

import com.kuizii.server.discovery.*;
import com.kuizii.server.net.IOManager;
import com.kuizii.server.net.NettyIOManager;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置默认服务
 *
 * @author renxiaodong
 */
@EnableConfigurationProperties(RegisterProperties.class)
@Configuration
public class RegisterServerAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean
    DefaultRegisterServer getRegisterServer(SecurityHandler securityHandler, IOManager ioManager) {

        RegisterEventListener registerEventListener = new NettyRegisterEventListener(securityHandler, ioManager);
        DefaultRegisterServer defaultRegisterServer = new DefaultRegisterServer(securityHandler, ioManager, registerEventListener);

        return defaultRegisterServer;
    }

    @Bean
    @ConditionalOnMissingBean
    SecurityHandler securityHandler() {
        DefaultSecurityHandler defaultSecurityHandler = new DefaultSecurityHandler();
        return defaultSecurityHandler;
    }

    @Bean
    @ConditionalOnMissingBean
    IOManager ioManager(RegisterProperties properties, ApplicationContext context) {
        NettyIOManager nettyIOManager = new NettyIOManager(properties,context);
//        nettyIOManager.init(properties);
        return nettyIOManager;
    }
}
